<?php
FLEA::loadClass('Controller_Base');
class Controller_Login extends Controller_Base {
	var $_modelUsersManager;
	var $_modelProductClasses;
	var $_modelProductsManager;
	var $_modelProductWords;
	function Controller_Login() {
		//load_language('ui, exception');
		$this->_modelUsersManager = & FLEA::getSingleton("Model_Users");
		$this->_modelProductClasses = & FLEA::getSingleton("Model_DB_ProductClasses");
		$this->_modelProductsManager = & FLEA::getSingleton("Model_Products");
		$this->_modelProductWords = & FLEA::getSingleton("Model_ProductWords");
	}
	function actionIndex() {
		$ui = & FLEA::initWebControls();
		if(!$_SESSION['jiraa']) {
			$this->actionLoginForm();
		} else {
			$this->actionLoginForm();
//			$loginform = '已在线';
			//根据用户角色给出菜单
		}
		$product_classes = $this->_modelProductClasses->findAllByField("parent",0);
		//取出默认定制的大类下面的相关信息
		$pcid = 1;
//		$productslist =  $this->_modelProductsManager->findBySql("select * from `products` where pcid='1'");
		
		
//		$product_words = $this->_modelProductWords->getProductWord(1);
//		$product_words = $this->_modelProductWords->findAll();
		include(TPL_DIR."/index.php");
	}
//输出登陆表单
	function actionLoginForm() {
		require_once(TPL_DIR."/login_area.php");
	}
	function actionRegisterForm() {
		$ui = & FLEA::initWebControls();
		$code .= $ui->control("label","lable_email",array('for'=>'email','caption'=>'电子信箱','class'=>'lable_text'),true);
		$code .= $ui->control("textbox","email",array('value'=>""),true);
		$code .= $ui->control("label","label_password",array('for'=>'password','caption'=>'密码','class'=>'label_text'),true);
		$code .= $ui->control("password","password",array('value'=>""),true);
		$code .= $ui->control("button","act_register",array('value'=>"注册"),true);
	}
	function actionRegisterCheck() {
		$user = $this->_modelUsersManager->findByEmail($_POST['email']);
		if(!$user) {
			//验证email
			echo "可以注册";
		} else {
			//
			echo "该email已注册,请换一个注册";
		}
	}
	function actionRegisterAct() {
		$user = $this->_modelUsersManager->findByEmail($_POST['email']);
		if(!$user) {
			$a_user = array(
				"username"=>$_POST['email'],
				"password"=>$_POST['password'],
				"email"=>$_POST['email']
			);
			$this->_modelUsersManager->create($a_user);
			echo true;
		} else {
			echo "该email已注册,请换一个注册";
		}
	}
	//登陆一个用户
	function actionLoginAct() {
		$imgcode =& FLEA::getSingleton('FLEA_Helper_ImgCode');
		do {
/*			if (!$imgcode->check($_POST['imgcode'])) {
				echo $msg = _T('ui_l_invalid_imgcode');
				break;
			}
*/
//			$imgcode->clear();
            /* @var $sysusers Model_SysUsers */
            $user = $this->_modelUsersManager->findByEmail($_POST['email']);
            if (!$user) {
//                $msg = _T('ui_l_invalid_username');
				echo "没有该用户";
				break;
            }
            if (!$this->_modelUsersManager->checkPassword($_POST['password'],
                $user[$this->_modelUsersManager->passwordField])) {
                //$msg = _T('ui_l_invalid_password');
				echo "密码错误";
                break;
            }
			$data = array();
			$data['email'] = $user[$this->_modelUsersManager->emailField];
			$data['uid'] = $user[$this->_modelUsersManager->primaryKey];
			$rbac =& FLEA::getSingleton('FLEA_Com_RBAC');
			$rbac->setUser($data, $this->_modelUsersManager->fetchRoles($user));
//			$_SESSION['LANG'] = $_POST['language'];
//			dump($_SESSION);
			echo "登陆成功";//提供登陆后菜单菜单
			echo "<input type='button' value='查看注册用户' id='act_look_users' />";
			//用户自有菜单初始化
//			echo "PL";
            // 重定向
//            redirect(url('BoDashboard'));

		} 
		while(false);
	}
	function actionImgCode() {
        $imgcode =& FLEA::getSingleton('FLEA_Helper_ImgCode');
        /* @var $imgcode FLEA_Helper_ImgCode */
        $imgcode->image();
    }
}
?>